
library(xlsx)
library(data.table)
library(RColorBrewer)
library(pheatmap)
library(readxl)


origcompounds=read_excel('./data/41586_2018_623_MOESM3_ESM.xlsx',10)
inhibitor=unique(origcompounds$inhibitor)
name2=sapply(strsplit(inhibitor,' '),head,1)
name2=gsub('-','_',name2)
origexpr=read_excel('./data/41586_2018_623_MOESM3_ESM.xlsx',8)



cols = colorRampPalette(c("#2544ce", "#5067ce", "#FAFAFA", "#e26161", "#d10202"))(1055)


  genes='MFN2'

expr=origexpr[origexpr$Symbol %in% genes,]



##plot ic50

res=data.frame(matrix(NaN,length(genes),length(inhibitor)))
rownames(res)=genes
colnames(res)=inhibitor
res_p=res
for (i in 1:length(genes)) {
  for (j in 1:length(inhibitor)) {
    cursubjs=intersect(names(expr),unlist(origcompounds[origcompounds$inhibitor %in% inhibitor[j],c(2)]))
    drug_resp=origcompounds$ic50[match(cursubjs,origcompounds$lab_id)]
    cur_expr=as.numeric(expr[match(genes[i],expr$Symbol),match(cursubjs,colnames(expr))])
    test1=cor.test(cur_expr,drug_resp,method = 'spearman')
    res[i,j]=test1$estimate
    res_p[i,j]=test1$p.value
  }
}


plotdata=res

plotdata[res_p>=0.05]=0
breaklist=seq(-max(abs(plotdata)),max(abs(plotdata)),length.out=1056)

filename=paste('./figures/Figure_2D.pdf',collapse='',sep='')
pheatmap(plotdata, 
         scale ='none' , cluster_rows = F, cluster_cols = T,show_rownames=T,
         cellwidth=5,cellheight=10,border_color = "black",
         color = cols,breaks=breaklist,fontsize_row = 8, fontsize_col = 6,
         clustering_distance_cols = "euclidean", clustering_method = "complete",
         main=paste('Vizome Drug Response IC50 vs. Expr \n(correlation score,white=non-significant correlation)', collapse='',sep=''),
         filename=filename) 
